package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.R
import com.youdianstar.app.entity.MethodBean
import com.youdianstar.app.common.DataCode
import com.youdianstar.app.common.ex.setThrottleListener


/**
 * File: PaymentMethodAdapter
 * Author: 82149 Create: 2021/11/26 17:26
 * Changes (from 2021/11/26)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class PaymentMethodAdapter(
    val context: Context, var data: MutableList<MethodBean>,
    val actionListener: ItemSelectListener
) : RecyclerView.Adapter<PaymentMethodAdapter.MethodHolder>() {
    interface ItemSelectListener{
        fun onNewCardAction()
        fun onItemSelectAction(index:Int, isChecked:Boolean)
    }
    class MethodHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val llContainer = itemView.findViewById<ConstraintLayout>(R.id.ll_pay_item)
        val ivIcon = itemView.findViewById<ImageView>(R.id.iv_card_icon)
        val tvInfo = itemView.findViewById<TextView>(R.id.tv_number)
        val tvDes = itemView.findViewById<TextView>(R.id.tv_des)
        val ivBox = itemView.findViewById<ImageView>(R.id.iv_card_enter)

        fun setIcon(resId: Int) {
            if (resId == 0) {
                ivIcon.visibility = View.INVISIBLE
            } else {
                ivIcon.visibility = View.VISIBLE
                ivIcon.setImageResource(resId)
            }
        }

        fun setContent(text: String) {
            tvInfo.text = text
        }

        fun setPayContent(text: String) {
            tvDes.text = text
        }
    }

    fun update(list: MutableList<MethodBean>) :Int{
        data.clear()
        data.addAll(list)
        val df = setDefaultSelected()
        notifyDataSetChanged()
        return df
    }

    fun updateMethodEnable(type:Int, isEnable:Boolean){
        if (data.isNotEmpty()){
            data.forEach { bean ->
                if (bean.type == type){
                    bean.isEnable = isEnable
                    bean.isCheck = false
                }
            }
            this.notifyDataSetChanged()
        }
    }

    /**
     * 获取选中支付类型
     * --选中类型对象
     */
    fun getSelectItem(): MethodBean? {
        val iterator = data.listIterator()
        while (iterator.hasNext()) {
            val item = iterator.next()
            if (item.isCheck) {
                return item
            }
        }
        return null
    }

    private fun setDefaultSelected():Int{
        var dfIndex = 0
        for ((index,e) in data.withIndex()){
            if (e.type != DataCode.Type_NewCard && e.isEnable && dfIndex ==0){
                e.isCheck = true
                dfIndex = index
            }else{
                e.isCheck = false
            }
        }
        return dfIndex
    }

    /**
     * 更新选中支付类型
     *
     * @param position index
     */
    private fun setCheckPayType(position: Int, item: MethodBean) {
        if (position == 0) {
            actionListener.onNewCardAction()
            return
        }
        val iterator = data.listIterator()
        while (iterator.hasNext()) {
            val next = iterator.next()
            if (next == item) {
                next.isCheck = !item.isCheck
                actionListener.onItemSelectAction(position,next.isCheck)
            } else {
                next.isCheck = false
            }
        }
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MethodHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_item_pay, parent, false)
        return MethodHolder(view)
    }

    override fun onBindViewHolder(holder: MethodHolder, position: Int) {
        val method = data[position]
        holder.setIcon(method.icon)
        holder.setContent(method.content)
        holder.setPayContent(method.des?:"")
        if (method.type == DataCode.Type_NewCard) {
            holder.ivBox.setImageResource(R.mipmap.ic_pay_right)
            holder.llContainer.setBackgroundResource(R.drawable.bg_pay_item_unselected)
            holder.llContainer.alpha = 1.0F
            holder.tvDes.visibility = View.VISIBLE
        } else {
            holder.ivBox.setImageResource(if (method.isCheck) R.mipmap.pay_box_selected else R.mipmap.pay_box_unselected)
            holder.llContainer.setBackgroundResource(if (method.isCheck) R.drawable.bg_pay_item_selected else R.drawable.bg_pay_item_unselected)
            holder.llContainer.alpha = if (method.isEnable) 1.0F else 0.4F
            holder.tvDes.visibility = View.GONE
        }

        holder.llContainer.setThrottleListener {
            data[holder.bindingAdapterPosition].let { item ->
                if (item.isEnable)
                    setCheckPayType(position, item)
            }
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }
}